﻿var FeedBackComment = function () {
    lstcomment = [];
    lstcommentsend = [];
    function round(value, decimals) {
        return Number(Math.round(value + 'e' + decimals) + 'e-' + decimals);
    }
    function convertdatatime(data) {
        var value = new Date
                    (
                         parseInt(data.replace(/(^.*\()|([+-].*$)/g, ''))
                    );
        var dat = value.getMonth() +
                                 1 +
                               "/" +
                   value.getDate() +
                               "/" +
               value.getFullYear();
        var datnew = dat.split('/');
        return datnew[1] + '/' + datnew[0] + '/' + datnew[2];
    }
    function checkexit(lstcommentedited, chitieu, donvi) {
        var kq = false;
        $.each(lstcommentedited, function (i) {
            if (parseInt(lstcommentedited[i].CHI_TIEU_ID) === parseInt(chitieu) && parseInt(lstcommentedited[i].DON_VI_ID) == parseInt(donvi)) {
                kq = true;
                return false;
            }
        });
        return kq;
    }
    function loadtable(namkh, initData) {
        $.ajax({
            url: initData.GetallCommentByDv,
            type: "GET",
            headers: {
                'VerificationToken': initData.VerificationToken
            },
            dataType: "json",
            data: {
                'namkh': namkh,
                'moc': 3,
                'flag': 0
            },
            async: false,
            success: function (data) {
                $('#lstdstruong').empty();
                $('#body').empty();
                if (data.length > 0 && data != "err") {
                    $('#Create').prop('disabled', false);
                    $('#gridContent').show();
                    lstcomment = [];
                    var mtpdv = 0;
                    $.each(data, function (i, v) {
                        var comment = v.COMMENT_DV;
                        if (comment.length > 40) {
                            comment = comment.substring(0, 40) + "...";
                        }
                        if (v.DON_VI_ID != mtpdv) {
                            $('#lstdstruong').append('<option value="' + v.DON_VI_ID + '">' + v.TenDV + '</option>');
                            mtpdv = v.DON_VI_ID;
                        }
                        var row = '<tr class="webgrid-row-style">\
                                        <td class="clsSTT" style="text-align: center; width: 30px;">\
                                            '+ (i + 1) + '\
                                        </td>\
                                        <td>\
                                            <div id="spnFullName" class="display-mode">\
                                                <span class="hidden-780 tenbc">' + $('#Namlapbc option:selected').text() + '</span>\
                                            </div>\
                                        </td>\
                                        <td class="clsUserName hidden-480">\
                                            <span id="spnUserName" class="display-mode timeline">' + v.TEN_CHI_TIEU + '</span>\
                                        </td>\
                                        <td class="clsUserName hidden-480">\
                                            <span id="spnUserName" class="display-mode timeline">\
                                                    <label title="' + v.COMMENT_DV + '">\
                                                        ' + comment + '\
                                                </label>\
                                            </span>\
                                        </td>\
                                        <td class="clsUserName hidden-480">\
                                            <span id="spnUserName" class="display-mode timeline">\
                                                ' + v.TenDV + '\
                                            </span>\
                                        </td>\
                                    </tr>';
                        $('#body').append(row);
                        lstcomment.push(v);
                    });
                    $('#bd').empty();
                    var dv = $('#lstdstruong').val();
                    $.each(lstcomment, function (i, v) {
                        if (v.DON_VI_ID == dv) {
                            var dc = "";
                            if (v.DieuChinh != null) {
                                dc = v.DieuChinh;
                            }
                            $('#bd').append('<tr chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '">\
                                <td>' + v.TEN_CHI_TIEU + '</td>\
                                 <td>' + v.DON_VI_TINH + '</td>\
                                <td><input type="text" disabled="disabled"  chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" class="e-input1 form-control" level="1" value="' + v.KeHoachDauNam + '" /></td>\
                                <td><input type="text" disabled="disabled"  chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" class="e-input1 form-control" level="2" value="' + dc + '" /></td>\
                                <td><input type="text"   chitieuid="' + v.CHI_TIEU_ID + '" class="e-input3 form-control" level="3" value="' + v.ThucHien + '" /></td>\
                                <td><input type="button" class="btn btn-danger commnetban"  chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" style="width: 90px;" data-html="true" data-placement="bottom" level="11" title="Nội dung comment"  value="Ý kiến" /></td>\
                                <td><button type="button" chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" style="width: 90px;" class="btn approve">Chấp nhận</button>\
                                <input type="button" class="btn btn-danger rejectnew1"  chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" style="width: 90px;"  data-html="true" data-placement="bottom" level="12" title="Nội dung comment"  value="Từ chối" /></td>\
                            </tr>');
                        }
                    });
                } else {
                    $('#Create').prop('disabled', true);
                    $('#gridContent').hide();
                }

            },
            error: function (jqXHR, textStatus, errorThrown) {
                window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
            },
            complete: function () {
            }
        });
        $.ajax({
            url: initData.CommentCheckChot,
            type: "GET",
            headers: {
                'VerificationToken': initData.VerificationToken
            },
            dataType: "json",
            data: {
                'namkh': namkh,
                'ban': initData.DonviID,
                'moc': 3
            },
            async: false,
            success: function (data) {
                $('#thongbao').text("");
                if (data.length > 0) {
                    var thongbao = "Danh sách các trường chưa phản hồi: ";
                    $.each(data, function (i, v) {
                        thongbao += v.DON_VI + ", ";
                    });
                    $('#thongbao').text(thongbao);
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
            },
            complete: function () {
            }
        });
        $.ajax({
            url: initData.CheckFeedBack,
            type: "GET",
            headers: {
                'VerificationToken': initData.VerificationToken
            },
            dataType: "json",
            data: {
                'namkh': namkh,
                'dvid': initData.DonviID,
            },
            success: function (data) {
                if (data == 0) {
                    $('#CreateNew').prop('disabled', true);
                    //$('#HuyChot').prop('disabled', false);
                } else {
                    $('#CreateNew').prop('disabled', false);
                    //$('#HuyChot').prop('disabled', true);
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
            },
            complete: function () {
            }
        });
        $.ajax({
            url: initData.CheckChotbyMocKSByBan,
            type: "GET",
            headers: {
                'VerificationToken': initData.VerificationToken
            },
            dataType: "json",
            data: {
                'namkh': namkh,
            },
            success: function (data) {
                if (data.length > 0) {
                    $('#tblds').show();
                    $('#dsbody').empty();
                    $.each(data, function (i, v) {
                        $('#dsbody').append('<tr class="webgrid-row-style"><td>\
                                          <div id="spnFullName" class="display-mode">\
                                           <span class="hidden-780 ">' + (i + 1) + '</span>\
                                         </div>\
                                </td>\
                                    <td>\
                                          <div id="spnFullName" class="display-mode">\
                                           <span class="hidden-780 ">' + v.DON_VI + '</span>\
                                         </div>\
                                </td></tr>\
                    ');
                    });
                } else {
                    $('#tblds').hide();
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
            },
            complete: function () {
            }
        });
    }
    var initLoadData = function (initData) {
        $.ajax({
            url: initData.GetallNamkehoach,
            type: "GET",
            headers: {
                'VerificationToken': initData.VerificationToken
            },
            dataType: "json",
            async: false,
            success: function (data) {

                var lst = [];
                $('#Namlapbc').empty();
                $('#Namlapbc').append('<option value="-1">[--Chọn năm kế hoạch--]</option>');
                $.each(data, function (i, v) {
                    if (v.STATUS_ID >= 40) {
                        $('#Namlapbc').append('<option value="' + v.NAM_KE_HOACH_ID + '">' + v.NAM_KE_HOACH + '</option>');
                        lst.push(v);
                    }
                });
                var namkh = $.cookie('mkhidchung');
                if (namkh != undefined) {
                    $('#Namlapbc').val(namkh);
                    loadtable(namkh, initData);
                    $.session.set('nkhid', namkh);
                }
                data = lst;
            },
            error: function (jqXHR, textStatus, errorThrown) {
                window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
            },
            complete: function () {
            }
        });
    };
    var initButtonClick = function (initData) {
        $('#Create').on('click', function () {
            lstcommentsend = [];
            $('#bd').empty();
            var dv = $('#lstdstruong').val();
            $.each(lstcomment, function (i, v) {
                if (v.DON_VI_ID == dv) {
                    var dc = "";
                    if (v.DieuChinh != null) {
                        dc = v.DieuChinh;
                    }
                    $('#bd').append('<tr chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '">\
                                <td>' + v.TEN_CHI_TIEU + '</td>\
                                 <td>' + v.DON_VI_TINH + '</td>\
                                <td><input type="text" disabled="disabled"  chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" class="e-input1 form-control" level="1" value="' + v.KeHoachDauNam + '" /></td>\
                                <td><input type="text" disabled="disabled"  chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" class="e-input1 form-control" level="2" value="' + dc + '" /></td>\
                                <td><input type="text"   chitieuid="' + v.CHI_TIEU_ID + '" class="e-input3 form-control" level="3" value="' + v.ThucHien + '" /></td>\
                                <td>'+ v.COMMENT_DV + '</td>\
                                <td><button type="button" chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" style="width: 90px;" class="btn approve">Chấp nhận</button>\
                                <input type="button" class="btn btn-danger rejectnew1"  chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" style="width: 90px;"  data-html="true" data-placement="bottom" level="12" title="Nội dung comment"  value="Từ chối" /></td>\
                            </tr>');
                }
            });
            $('#myModal').modal('show');
        });
        $('.reject').live('click', function () {
            $(this).popover({
                trigger: 'manual'
            });
            if ($(this).hasClass('pop')) {
                $(this)
                    .popover('hide')
                    .removeClass('pop');
            } else {
                var chitieu = $(this).attr('chitieuid');
                var donvi = $(this).attr('donviid');
                var contentnew = '<div class="row">\
            <div class="col-md-12">\
                <textarea class="form-control comment" chitieuid="' + chitieu + '" donviid="' + donvi + '" level="13" rows="6" ></textarea>\
            </div>\
            </div>\
            <br />\
            <div class="row">\
            <center>\
                <button type="button" chitieuid="' + chitieu + '"  donviid="' + donvi + '" class="btn ghinhan">Ghi nhận</button>\
                <button type="button" chitieuid="' + chitieu + '" donviid="' + donvi + '" class="btn huybo">Huỷ</button>\
            </center>\
            </div>';
                $(this)
                    .attr('data-content', contentnew)
                    .popover('show')
                    .addClass('pop');
            }

        });
        $('.reject').live('hide.bs.popover', function () {
            var chitieu = $(this).attr('chitieuid');
            var donvi = $(this).attr('donviid');
            var giatri = $('[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="3"]').val();
            var comment = $('[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="13"]').val();
            $.each(lstcommentsend, function (i) {
                if (lstcommentsend[i].CHI_TIEU_ID === chitieu && lstcommentsend[i].DON_VI_ID == donvi) {
                    lstcommentsend.splice(i, 1);
                    return false;
                }
            });
            if (comment.trim() != "") {
                var obj = {
                    'COMMENT_ID': 0,
                    'BAN_ID': 0,
                    'CHI_TIEU_ID': chitieu,
                    'COMMENT_BAN': comment,
                    'DON_VI_ID': donvi,
                    'GIA_TRI': giatri,
                    'COMMENT_DV': '',
                    'STATUS_ID': 0,
                    'IS_ACTIVE': 1,
                    'MOC': 3,
                    'NAM_KE_HOACH_ID': $('#Namlapbc').val()
                }
                lstcommentsend.push(obj);
                $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="12"]').val("Warning");
                $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="12"]').removeClass("btn-danger");
                $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="12"]').addClass("btn-warning");
            } else {
                $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="12"]').val("Từ chối");
                $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="12"]').removeClass("btn-warning");
                $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="12"]').addClass("btn-danger");
            }
        });
        $('.reject').live('shown.bs.popover', function () {
            var chitieu = $(this).attr('chitieuid');
            var donvi = $(this).attr('donviid');
            var comment = "";
            $.each(lstcommentsend, function (i, v) {
                if (v.CHI_TIEU_ID == chitieu && v.DON_VI_ID == donvi) {
                    comment = v.COMMENT_BAN;
                    return false;
                }
            });
            $('[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="13"]').text(comment);
        });
        $('.rejectnew1').live('click', function () {
            $(this).popover({
                trigger: 'manual'
            });
            if ($(this).hasClass('pop')) {
                $(this)
                    .popover('hide')
                    .removeClass('pop');
            } else {
                var chitieu = $(this).attr('chitieuid');
                var donvi = $(this).attr('donviid');
                var contentnew = '<div class="row">\
            <div class="col-md-12">\
                <textarea class="form-control comment" chitieuid="' + chitieu + '" donviid="' + donvi + '" level="13" rows="6" ></textarea>\
            </div>\
            </div>\
            <br />\
            <div class="row">\
            <center>\
                <button type="button" chitieuid="' + chitieu + '"  donviid="' + donvi + '" class="btn ghinhannew1">Ghi nhận</button>\
                <button type="button" chitieuid="' + chitieu + '" donviid="' + donvi + '" class="btn huybonew1">Huỷ</button>\
            </center>\
            </div>';
                $(this)
                    .attr('data-content', contentnew)
                    .popover('show')
                    .addClass('pop');
            }

        });
        $('.rejectnew1').live('hide.bs.popover', function () {
            var chitieu = $(this).attr('chitieuid');
            var donvi = $(this).attr('donviid');
            var giatri = $('[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="3"]').val();
            var comment = $('[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="13"]').val();
            $.each(lstcommentsend, function (i) {
                if (lstcommentsend[i].CHI_TIEU_ID === chitieu && lstcommentsend[i].DON_VI_ID == donvi) {
                    lstcommentsend.splice(i, 1);
                    return false;
                }
            });
            if (comment.trim() != "") {
                var obj = {
                    'COMMENT_ID': 0,
                    'BAN_ID': 0,
                    'CHI_TIEU_ID': chitieu,
                    'COMMENT_BAN': comment,
                    'DON_VI_ID': donvi,
                    'GIA_TRI': giatri,
                    'COMMENT_DV': '',
                    'STATUS_ID': 0,
                    'IS_ACTIVE': 1,
                    'MOC': 3,
                    'NAM_KE_HOACH_ID': $('#Namlapbc').val()
                }
                lstcommentsend.push(obj);
                $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="12"]').val("Warning");
                $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="12"]').removeClass("btn-danger");
                $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="12"]').addClass("btn-warning");
            } else {
                $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="12"]').val("Từ chối");
                $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="12"]').removeClass("btn-warning");
                $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="12"]').addClass("btn-danger");
            }
        });
        $('.rejectnew1').live('shown.bs.popover', function () {
            var chitieu = $(this).attr('chitieuid');
            var donvi = $(this).attr('donviid');
            var comment = "";
            $.each(lstcommentsend, function (i, v) {
                if (v.CHI_TIEU_ID == chitieu && v.DON_VI_ID == donvi) {
                    comment = v.COMMENT_BAN;
                    return false;
                }
            });
            $('[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="13"]').text(comment);
        });
        $('.commnetban').live('click', function () {
            $(this).popover({
                trigger: 'manual'
            });
            if ($(this).hasClass('pop')) {
                $(this)
                    .popover('hide')
                    .removeClass('pop');
            } else {
                var chitieu = $(this).attr('chitieuid');
                var contentnew = '<div class="row">\
                <div class="col-md-12">\
                    <textarea class="form-control comment" chitieuid="' + chitieu + '"level="10" rows="8" ></textarea>\
                </div>\
                </div>';
                $(this)
                    .attr('data-content', contentnew)
                    .popover('show')
                    .addClass('pop');
            }

        });
        $('.commnetban').live('shown.bs.popover', function () {
            var chitieu = $(this).attr('chitieuid');
            var comment = "";
            $.each(lstcomment, function (i, v) {
                if (v.CHI_TIEU_ID == chitieu) {
                    comment = v.COMMENT_DV;
                    return false;
                }
            });
            $('[chitieuid="' + chitieu + '"][level="10"]').text(comment);
        })
        $('#Phanhoi').live('click', function () {
            if (lstcommentsend.length == 0) {
                window.notice('Không có comment cho đơn vị nào.', window.notice_error);
            } else {
                $.ajax({
                    url: initData.UpdateCommentbyBan,
                    type: "POST",
                    headers: {
                        'VerificationToken': initData.VerificationToken
                    },
                    dataType: "json",
                    data: JSON.stringify({
                        'cmt': lstcommentsend,
                    }),
                    contentType: 'application/json',
                    success: function (data) {
                        window.notice('Bạn đã gửi phản hồi tới đơn vị thành công', window.notice_success);
                        loadtable($('#Namlapbc').val(), initData);
                        $('#myModal').modal('hide');
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                    },
                    complete: function () {
                    }
                });
            }
        });
        $('.ghinhan').live('click', function () {
            var chitieu = $(this).attr('chitieuid');
            var donvi = $(this).attr('donviid');
            $('[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="12"]').trigger('click');
        });
        $('.huybo').live('click', function () {
            var chitieu = $(this).attr('chitieuid');
            var donvi = $(this).attr('donviid');
            $('[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="13"]').val('');
            $('[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="12"]').trigger('click');
        });
        $('.ghinhannew1').live('click', function () {
            var chitieu = $(this).attr('chitieuid');
            var donvi = $(this).attr('donviid');
            $('[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="12"]').trigger('click');
        });
        $('.huybonew').live('click', function () {
            var chitieu = $(this).attr('chitieuid');
            var donvi = $(this).attr('donviid');
            $('[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="13"]').val('');
            $('[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="12"]').trigger('click');
        });
        $('.approve').live('click', function () {
            var chitieu = $(this).attr('chitieuid');
            var donvi = $(this).attr('donviid');
            var nkh = $('#Namlapbc').val();
            var moc = 3;
            $.ajax({
                url: initData.UpdateCommentApprove,
                type: "POST",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                data: {
                    'namkh': nkh,
                    'chitieu': chitieu,
                    'donvi': donvi,
                    'moc': moc
                },
                success: function (data) {
                    //window.notice('Bạn đã gửi phản hồi tới đơn vị thành công', window.notice_success);
                    //loadtable($('#Namlapbc').val(), initData);
                    //$('#myModal').modal('hide');
                    if (data == 1) {
                        window.notice('Bạn đã approve thành công', window.notice_success);
                        $('tr[chitieuid="' + chitieu + '"][donviid="' + donvi + '"]').remove();
                        loadtable($('#Namlapbc').val(), initData);
                    } else {
                        window.notice('Bạn đã approve thất bại', window.notice_error);
                    }

                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {
                }
            });
        })
        $('#Namlapbc').on('change', function () {
            var data = $(this).val();
            if (data != -1) {
                loadtable(data, initData);
            }
            $.cookie('mkhidchung', $(this).val());
            $.session.set('nkhid', data);

        })
        $('#lstdstruong').live('change', function () {
            var dv = $(this).val();
            $('#bd').empty();
            $.each(lstcomment, function (i, v) {
                if (v.DON_VI_ID == dv) {
                    var dc = "";
                    if (v.DieuChinh != null) {
                        dc = v.DieuChinh;
                    }
                    $('#bd').append('<tr chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '">\
                                <td>' + v.TEN_CHI_TIEU + '</td>\
                                 <td>' + v.DON_VI_TINH + '</td>\
                                <td><input type="text" disabled="disabled"  chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" class="e-input1 form-control" level="1" value="' + v.KeHoachDauNam + '" /></td>\
                                <td><input type="text" disabled="disabled"  chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" class="e-input1 form-control" level="2" value="' + dc + '" /></td>\
                                <td><input type="text"   chitieuid="' + v.CHI_TIEU_ID + '" class="e-input3 form-control" level="3" value="' + v.ThucHien + '" /></td>\
                                <td>'+ v.COMMENT_DV + '</td>\
                                <td><button type="button" chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" style="width: 90px;" class="btn approve">Chấp nhận</button>\
                                <input type="button" class="btn btn-danger rejectnew1"  chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" style="width: 90px;"  data-html="true" data-placement="bottom" level="12" title="Nội dung comment"  value="Từ chối" /></td>\
                            </tr>');
                }
            });
        })

        //FeedBack
        var stus = 10;
        var fl = 0;
        function loaddatatotable(nhomchitieu, nhomdonvi, dt, initData, nkhid, flag) {
            var lstchitieucap2;
            var lstdonvi = [];
            $.ajax({
                url: initData.GetallChitieubynamchitieu,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                async: false,
                data: {
                    'namchitieuid': nkhid,
                    'madonvi': initData.DonviID
                },
                success: function (data) {
                    lstchitieucap2 = data;
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {

                }
            });
            if (flag == 0) {
                $.ajax({
                    url: initData.GetAllDonViByNhomID,
                    type: "GET",
                    headers: {
                        'VerificationToken': initData.VerificationToken
                    },
                    dataType: "json",
                    async: false,
                    data: {
                        'nhomdvid': nhomdonvi
                    },
                    success: function (data) {
                        lstdonvi = data;
                        if (data.length >= 4 && data.length <= 8) {
                            $('#tbdata').css('width', '3000px');
                            $('.chitieun').css('width', '12%');
                            $('.dvt').css('width', '3%');
                            chitieu = 12;
                            dvt = 3;
                        }
                        if (data.length < 4) {
                            $('#tbdata').css('width', '1110px');
                            $('.chitieun').css('width', '22%');
                            $('.dvt').css('width', '7%');
                            chitieu = 22;
                            dvt = 7;
                        }
                        if (data.length >= 9) {
                            $('#tbdata').css('width', '5000px');
                            $('.chitieun').css('width', '10%');
                            $('.dvt').css('width', '2%');
                            chitieu = 10;
                            dvt = 2;
                        }

                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                    },
                    complete: function () {

                    }
                });
            } else {
                $.ajax({
                    url: initData.GetDonViID,
                    type: "GET",
                    headers: {
                        'VerificationToken': initData.VerificationToken
                    },
                    dataType: "json",
                    async: false,
                    data: {
                        'donviid': nhomdonvi
                    },
                    success: function (data) {
                        lstdonvi.push(data);
                        $('#tbdata').css('width', '1110px');

                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                    },
                    complete: function () {

                    }
                });
            }
            $('#hhh').empty();
            $('#bdy').empty();
            var dt = " <tr>";
            dt += '<th rowspan="2" class="chitieun" style=" width: ' + chitieu + '%;">' + $('#nhomchitieu option:selected').text().toUpperCase() + '</th>\
               <th rowspan="2" class="dvt" style=" width: '+ dvt + '%;">Đơn vị tính</th>\
               <th colspan="5" style="background-color:#C9E8D1" class="ct1">ĐHQG</th>';
            var dx = '<tr>\
                    <th>KH</th>\
                    <th class="dc">ĐC</th>\
                    <th>Tổng TH</th>\
                    <th>% (KH)</th>\
                    <th class="dc">% (ĐC)</th>';
            $.each(lstdonvi, function (i, v) {
                dt += '<th  colspan="4" style="background-color:#C9E8D1" class="ct1 dvcolum">' + v.TenVietTat + '</th>';
                dx += '<th>KH</th>\
                    <th class="ctcolum">KT</th>\
                    <th>KN</th>\
                    <th>%</th>';
            })
            dx += "</tr>";
            dt += "</tr>";
            $('#hhh').append(dt);
            $('#hhh').append(dx);
            $.each(lstchitieucap2, function (i, v) {
                if (v.CAP == 2 && v.MA_CAP1 == nhomchitieu) {
                    var dt = "<tr>";
                    dt += '<td style="font-weight:bold;">' + v.TEN_CHI_TIEU + '</td>\
                                <td></td>\
                                <td></td>\
                                <td class="dc"></td>\
                                <td></td>\
                                <td></td>\
                                <td class="dc"></td>';
                    $.each(lstdonvi, function (i, v) {
                        dt += '<td></td>\
                                <td class="ctcolum"></td>\
                                <td></td>\
                    <td></td>';
                    });
                    dt += '</tr>';
                    $('#bdy').append(dt);
                }
                if (v.CAP == 3 && v.MA_CAP1 == nhomchitieu) {
                    var dt = "<tr>";
                    dt += '<td>' + v.TEN_CHI_TIEU + '</td>\
                        <td style="text-align: center;">' + v.DON_VI_TINH + '</td>\
                                <td><input type="text" chitieuid="' + v.CHI_TIEU_ID + '" disabled="disabled" class="e-input1 form-control restartall" level="3" value="" /></td>\
                                <td class="dc"><input type="text" chitieuid="' + v.CHI_TIEU_ID + '" disabled="disabled" class="e-input1 form-control restartall" level="5" value="" /></td>\
                                <td><input type="text" chitieuid="' + v.CHI_TIEU_ID + '" disabled="disabled" class="e-input1 form-control restartall" level="15" value="" /></td>\
                                <td><input type="text" chitieuid="' + v.CHI_TIEU_ID + '" disabled="disabled" class="e-input1 form-control restartall" level="4" value="" /></td>\
                                <td class="dc"><input type="text" chitieuid="' + v.CHI_TIEU_ID + '" disabled="disabled" class="e-input1 form-control restartall" level="6" value="" /></td>\
                                ';
                    $.each(lstdonvi, function (y, z) {
                        dt += '<td><input type="text" chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + z.DonViId + '" disabled="disabled" class="e-input1 form-control restartall" level="1" value="" /></td>\
                                <td class="ctcolum"><input type="text" chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + z.DonViId + '" disabled="disabled" class="e-input1 form-control restartall " level="8" value="" /></td>\
                           <td>\
                           <input type="text" chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + z.DonViId + '" data-html="true" data-placement="bottom" title="Nội dung comment" class="e-input1 form-control restartall" level="2" value="" />';

                        dt += '<input type="button" class="btn btn-danger rejectnew"  chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + z.DonViId + '" data-html="true" data-placement="bottom" level="11" title="Nội dung comment"  value="Từ chối" />';
                        dt += '      </td>\
                           <td><input type="text" chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + z.DonViId + '" disabled="disabled" class="e-input1 form-control restartall" level="7" value="" /></td>';
                    });
                    dt += '</tr>';
                    $('#bdy').append(dt);
                }
            });
            $.ajax({
                url: initData.GetKehoachbyNamkehoach,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                async: false,
                data: {
                    'namkhid': $.session.get('nkhid'),
                    'statusid': 31,
                    'flag': 0
                },
                success: function (data) {
                    $.each(data, function (i, v) {
                        $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="1"]').val(v.GIA_TRI);
                    })
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {
                }
            });
            $.ajax({
                url: initData.GetKehoachbyNamkehoach,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                async: false,
                data: {
                    'namkhid': $.session.get('nkhid'),
                    'statusid': 61,
                    'flag': 0
                },
                success: function (data) {
                    if (data.length == 0) {
                        $('.dc').hide();
                        $('.ct1').attr('colspan', 3);
                    } else {
                        $('.dc').show();
                        $('.ct1').attr('colspan', 5);
                    }
                    $.each(data, function (i, v) {
                        $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="1"]').val(v.GIA_TRI);
                    })
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {
                }
            });
            if (flag == 0) {
                $.ajax({
                    url: initData.Getkehoachbymockiemsoat,
                    type: "GET",
                    headers: {
                        'VerificationToken': initData.VerificationToken
                    },
                    dataType: "json",
                    async: false,
                    data: {
                        'namkh': $.session.get('nkhid'),
                        'nhomdonvi': nhomdonvi,
                        'flag': '0'
                    },
                    success: function (data) {
                        $.each(data, function (i, v) {
                            $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="2"]').val(v.GIA_TRI);
                            var b = $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="2"]').val();
                            if (b != "" && b != "0" && b != undefined) {
                                var a = $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="1"]').val();
                                if (a != "" && a != "0" && a != undefined) {
                                    var c = round((parseInt(b) / parseInt(a)) * 100, 2);
                                    $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="7"]').val(c);
                                }
                            }
                        });
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                    },
                    complete: function () {
                    }
                });
                $.ajax({
                    url: initData.Getkehoachbymockiemsoatbefo,
                    type: "GET",
                    headers: {
                        'VerificationToken': initData.VerificationToken
                    },
                    dataType: "json",
                    async: false,
                    data: {
                        'namkh': $.session.get('nkhid'),
                        'nhomdonvi': nhomdonvi,
                        'flag': '0'
                    },
                    success: function (data) {

                        if (data.length > 0) {
                            $('.dvcolum').attr('colspan', 4);
                            $('.ctcolum').show();
                            $.each(data, function (i, v) {
                                $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="8"]').val(v.GIA_TRI);
                            });
                        } else {
                            $('.dvcolum').attr('colspan', 3);
                            $('.ctcolum').hide();
                        }
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                    },
                    complete: function () {
                    }
                });
            } else {
                $.ajax({
                    url: initData.Getkehoachbymockiemsoat,
                    type: "GET",
                    headers: {
                        'VerificationToken': initData.VerificationToken
                    },
                    dataType: "json",
                    async: false,
                    data: {
                        'namkh': $.session.get('nkhid'),
                        'nhomdonvi': nhomdonvi,
                        'flag': '1'
                    },
                    success: function (data) {
                        $.each(data, function (i, v) {
                            $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="2"]').val(v.GIA_TRI);
                            var b = $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="2"]').val();
                            if (b != "" && b != "0" && b != undefined) {
                                var a = $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="1"]').val();
                                if (a != "" && a != "0" && a != undefined) {
                                    var c = round((parseInt(b) / parseInt(a)) * 100, 2);
                                    $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="7"]').val(c);
                                }
                            }
                        });
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                    },
                    complete: function () {
                    }
                });
                $.ajax({
                    url: initData.Getkehoachbymockiemsoatbefo,
                    type: "GET",
                    headers: {
                        'VerificationToken': initData.VerificationToken
                    },
                    dataType: "json",
                    async: false,
                    data: {
                        'namkh': $.session.get('nkhid'),
                        'nhomdonvi': nhomdonvi,
                        'flag': '1'
                    },
                    success: function (data) {
                        if (data.length > 0) {
                            $('.dvcolum').attr('colspan', 4);
                            $('.ctcolum').show();
                            $.each(data, function (i, v) {
                                $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="8"]').val(v.GIA_TRI);
                            });
                        } else {
                            $('.dvcolum').attr('colspan', 3);
                            $('.ctcolum').hide();
                        }
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                    },
                    complete: function () {
                    }
                });
            }
            //$.ajax({
            //    url: initData.GetSumKH,
            //    type: "GET",
            //    headers: {
            //        'VerificationToken': initData.VerificationToken
            //    },
            //    dataType: "json",
            //    async: false,
            //    data: {
            //        'namkhid': $.session.get('nkhid'),
            //        'statusid': 31,
            //    },
            //    success: function (data) {
            //        $.each(data, function (i, v) {
            //            $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="3"]').val(v.GIA_TRI);
            //        })
            //    },
            //    error: function (jqXHR, textStatus, errorThrown) {
            //        window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
            //    },
            //    complete: function () {
            //    }
            //});
            $.ajax({
                url: initData.GetKehoachByNamKeHoach_NhomDV_NhomCT,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                async: false,
                data: {
                    'namkh': $.session.get('nkhid'),
                    'status': 0,
                    'nhomdv': nhomdonvi,
                    'nhomct': nhomchitieu,
                    'flag': 0
                },
                success: function (data) {
                    $.each(data, function (i, v) {
                        $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="3"]').val(numeral(v.GIA_TRI).format('0,0'));
                    })

                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {
                }
            });
            $.ajax({
                url: initData.GetSumKH,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                async: false,
                data: {
                    'namkhid': $.session.get('nkhid'),
                    'statusid': 61,
                },
                success: function (data) {
                    $.each(data, function (i, v) {
                        $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="5"]').val(v.GIA_TRI);
                    })
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {
                }
            });
            $.ajax({
                url: initData.GetSumKHTH,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                async: false,
                data: {
                    'namkhid': $.session.get('nkhid'),
                    'flag': 1
                },
                success: function (data) {
                    $.each(data, function (i, v) {
                        var chinhthuc = $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="3"]').val();
                        $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="4"]').val(v.GIA_TRI);
                        $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="15"]').val(v.GIA_TRI);
                        if (chinhthuc != "" && chinhthuc != "0" && chinhthuc != undefined) {
                            var ht1 = $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="4"]').val();
                            if (ht1 != "" && ht1 != "0" && ht1 != undefined) {
                                var c = round((parseInt(ht1) / parseInt(chinhthuc)) * 100, 2);
                                $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="4"]').val(c);
                            }
                        }
                        var dinhhuong = $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="5"]').val();
                        if (dinhhuong != "" && dinhhuong != "0" && dinhhuong != undefined) {
                            $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="6"]').val(v.GIA_TRI);
                            var ht2 = $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="6"]').val();
                            if (ht2 != "" && ht2 != "0" && ht2 != undefined) {
                                var c = round((parseInt(ht2) / parseInt(dinhhuong)) * 100, 2);
                                $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="6"]').val(c);
                            }
                        }
                    })
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {
                }
            });
            $.ajax({
                url: initData.GetallCommentByBan,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                async: false,
                data: {
                    'namkh': $.session.get('nkhid'),
                    'moc': 3,
                    'flag': 0
                },
                success: function (data) {
                    lstcommentnew = [];
                    lstcommentsendnew = [];
                    lstcommentsendupdate = [];
                    lstcommentedited = [];
                    $.each(data, function (i, v) {
                        if (v.STATUS_ID == 1) {
                            //$('input[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="11"]').val("Edited");
                            //$('input[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="11"]').removeClass("btn-warning");
                            //$('input[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="11"]').addClass("btn-success");
                            $('input[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="2"]').css('background-color', '#009999');
                            $('input[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="2"]').attr('title', 'Ý kiến của đơn vị: ' + v.COMMENT_DV);
                            lstcommentedited.push(v);

                        } else {
                            if (v.STATUS_ID == 3) {
                                $('input[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="2"]').css('background-color', '#5bc0de');
                                $('input[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="2"]').attr('title', 'Ý kiến của đơn vị: ' + v.COMMENT_DV);
                                lstcommentedited.push(v);
                            } else {
                                $('input[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="2"]').css('background-color', '#dfba49');
                                $('input[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="2"]').attr('title', 'Ý kiến của ban: ' + v.COMMENT_BAN);
                                $('input[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="11"]').prop('disabled', true);
                                lstcommentnew.push(v);
                            }
                        }

                    });
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {
                }
            });
            $("#tbdata").tableHeadFixer({ "left": 2 });
            $('input[level="1"]').each(function (i, v) {
                if ($(this).val() == "" || $(this).val() == null) {
                    var chitieu = $(this).attr('chitieuid');
                    var donviid = $(this).attr('donviid');
                    $('input[chitieuid="' + chitieu + '"][donviid="' + donviid + '"][level="11"]').hide();
                }
            })
            if (flag != 0) {
                $('.chitieun').css('width', '22%');
                $('.dvt').css('width', '7%');
                //$('.dvcolum').attr('colspan',4);
            } else {
                //$('.dvcolum').attr('colspan', 5);
            }

        }

        $('#CreateNew').on('click', function () {
            stus = 10;
            var namkehoachid = $('#Namlapbc').val();
            $.ajax({
                url: initData.GetDonViByNhom,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                data: {
                    'status': false
                },
                success: function (data) {
                    $('#donvi').empty();
                    $.each(data, function (i, v) {
                        $('#donvi').append('<option value="' + v.DonViId + '">' + v.TenDonVi + '</option>');
                    });
                    //if (initData.QUYENGIAMSAT == 'False') {
                    //    $("#donvi option").each(function () {
                    //        if ($(this).val() != initData.DonviID) {
                    //            $(this).remove();
                    //        }
                    //    });
                    //    $('#donvi').val(initData.DonviID);
                    //}
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {
                }
            });
            $.ajax({
                url: initData.GetallChitieubynamchitieu,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                async: false,
                data: {
                    'namchitieuid': namkehoachid,
                    'madonvi': initData.DonviID
                },
                success: function (data) {
                    lstnhomchitieu = data;
                    $('#nhomchitieu').empty();
                    $.each(data, function (i, v) {
                        if (v.CAP == 1) {
                            $('#nhomchitieu').append('<option value="' + v.MA_CAP1 + '">' + v.TEN_CHI_TIEU + '</option>')
                        }
                    });

                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {

                }
            });
            $.ajax({
                url: initData.Getallnhomdonvi,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                async: false,
                data: {
                    'namchitieuid': namkehoachid
                },
                success: function (data) {
                    $('#nhomdonvi').empty();
                    $.each(data, function (i, v) {
                        $('#nhomdonvi').append('<option value="' + v.NhomDonViId + '">' + v.TenNhomDonVi + '</option>')
                    });

                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {

                }
            });
            //if (initData.QUYENGIAMSAT == 'False') {
            //    $('#nhomdonvi').prop('disabled', 'disabled');
            //    $('#donvi').prop('disabled', 'disabled');
            //    fl = 1;
            //    $('.chn').hide();
            //    loaddatatotable($('#nhomchitieu').val(), initData.DonviID, stus, initData, $.session.get('nkhid'), fl);
            //} else {
            $('.chn').show();
            loaddatatotable($('#nhomchitieu').val(), $('#nhomdonvi').val(), stus, initData, namkehoachid, 0);
            // }

            $('#Update').show();
            $('#Chot').show();
            $('#myModalNew').modal('show');
        });
        $('.rejectnew').live('click', function () {
            $(this).popover({
                trigger: 'manual'
            });
            if ($(this).hasClass('pop')) {
                $(this)
                    .popover('hide')
                    .removeClass('pop');
            } else {
                var chitieu = $(this).attr('chitieuid');
                var donvi = $(this).attr('donviid');
                var contentnew = '<div class="row">\
            <div class="col-md-12">\
                <textarea class="form-control comment" chitieuid="' + chitieu + '" donviid="' + donvi + '" level="10" rows="6" ></textarea>\
            </div>\
            </div>\
            <br />\
            <div class="row">\
            <center>\
                <button type="button" chitieuid="' + chitieu + '"  donviid="' + donvi + '" class="btn ghinhan">Ghi nhận</button>\
                <button type="button" chitieuid="' + chitieu + '" donviid="' + donvi + '" class="btn huybo">Huỷ</button>\
            </center>\
            </div>';
                $(this)
                    .attr('data-content', contentnew)
                    .popover('show')
                    .addClass('pop');
            }

        });
        $('.rejectnew').live('hide.bs.popover', function () {
            var chitieu = $(this).attr('chitieuid');
            var donvi = $(this).attr('donviid');
            var comment = $('[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="10"]').val();
            if (!checkexit(lstcommentedited, chitieu, donvi)) {
                $.each(lstcommentsendnew, function (i) {
                    if (lstcommentsendnew[i].CHI_TIEU_ID === chitieu && lstcommentsendnew[i].DON_VI_ID == donvi) {
                        lstcommentsendnew.splice(i, 1);
                        return false;
                    }
                });
                if (comment.trim() != "") {
                    var obj = {
                        'COMMENT_ID': 0,
                        'BAN_ID': initData.DonviID,
                        'CHI_TIEU_ID': chitieu,
                        'COMMENT_BAN': comment,
                        'DON_VI_ID': donvi,
                        'GIA_TRI': '',
                        'COMMENT_DV': '',
                        'STATUS_ID': 0,
                        'IS_ACTIVE': 1,
                        'MOC': 3,
                        'NAM_KE_HOACH_ID': $('#Namlapbc').val()
                    }
                    lstcommentsendnew.push(obj);
                    $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="11"]').val("Warning");
                    $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="11"]').removeClass("btn-danger");
                    $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="11"]').addClass("btn-warning");
                } else {
                    $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="11"]').val("Từ chối");
                    $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="11"]').removeClass("btn-warning");
                    $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="11"]').addClass("btn-danger");
                }
            } else {
                $.each(lstcommentsendupdate, function (i) {
                    if (lstcommentsendupdate[i].CHI_TIEU_ID === chitieu && lstcommentsendupdate[i].DON_VI_ID == donvi) {
                        lstcommentsendupdate.splice(i, 1);
                        return false;
                    }
                });
                if (comment.trim() != "") {
                    var obj = {
                        'COMMENT_ID': 0,
                        'BAN_ID': initData.DonviID,
                        'CHI_TIEU_ID': chitieu,
                        'COMMENT_BAN': comment,
                        'DON_VI_ID': donvi,
                        'GIA_TRI': '',
                        'COMMENT_DV': '',
                        'STATUS_ID': 0,
                        'IS_ACTIVE': 1,
                        'MOC': 3,
                        'NAM_KE_HOACH_ID': $('#Namlapbc').val()
                    }
                    lstcommentsendupdate.push(obj);
                    $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="11"]').val("Warning");
                    $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="11"]').removeClass("btn-danger");
                    $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="11"]').addClass("btn-warning");
                } else {
                    $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="11"]').val("Từ chối");
                    $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="11"]').removeClass("btn-warning");
                    $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="11"]').addClass("btn-danger");
                }
            }
        })
        $('.rejectnew').live('shown.bs.popover', function () {
            var chitieu = $(this).attr('chitieuid');
            var donvi = $(this).attr('donviid');
            var comment = "";
            $.each(lstcommentnew, function (i, v) {
                if (v.CHI_TIEU_ID == chitieu && v.DON_VI_ID == donvi) {
                    comment = v.COMMENT_BAN;
                    return false;
                }
            });
            if (comment == "") {
                $.each(lstcommentsendnew, function (i, v) {
                    if (v.CHI_TIEU_ID == chitieu && v.DON_VI_ID == donvi) {
                        comment = v.COMMENT_BAN;
                        return false;
                    }
                });
            }
            $('[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="10"]').text(comment);
        })
        $('.ghinhan').live('click', function () {
            var chitieu = $(this).attr('chitieuid');
            var donvi = $(this).attr('donviid');
            $('[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="11"]').trigger('click');
        });
        $('.huybo').live('click', function () {
            var chitieu = $(this).attr('chitieuid');
            var donvi = $(this).attr('donviid');
            $('[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="10"]').val('');
            $('[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="11"]').trigger('click');
        });
        $('#SendComment').live('click', function () {
            if (lstcommentsendnew.length == 0 && lstcommentsendupdate.length == 0) {
                window.notice('Không có comment cho đơn vị nào.', window.notice_error);
            } else {
                if (lstcommentsendnew.length > 0) {
                    $.ajax({
                        url: initData.SaveCommnet,
                        type: "POST",
                        headers: {
                            'VerificationToken': initData.VerificationToken
                        },
                        dataType: "json",
                        data: JSON.stringify({
                            'cmt': lstcommentsendnew,
                        }),
                        contentType: 'application/json',
                        success: function (data) {
                            if (data == true) {
                                $('#myModalNew').modal('hide');
                            } else {
                                window.notice('Bạn đã gửi phản hồi tới đơn vị thất bại', window.notice_error);
                            }
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                        },
                        complete: function () {
                        }
                    });
                }
                if (lstcommentsendupdate.length > 0) {
                    $.ajax({
                        url: initData.UpdateCommentbyBan,
                        type: "POST",
                        headers: {
                            'VerificationToken': initData.VerificationToken
                        },
                        dataType: "json",
                        data: JSON.stringify({
                            'cmt': lstcommentsendupdate,
                        }),
                        contentType: 'application/json',
                        success: function (data) {

                            loadtable($('#Namlapbc').val(), initData);
                            $('#myModal').modal('hide');
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                        },
                        complete: function () {
                        }
                    });
                }
                window.notice('Bạn đã gửi phản hồi tới đơn vị thành công', window.notice_success);
            }
        })
        $('input[type=radio][name=inlineRadioOptions]').change(function () {
            var dt = $(this).val();
            if (dt == 'nhom') {
                $('#donvi').prop('disabled', 'disabled');
                $('#nhomdonvi').prop('disabled', false);
                fl = 0;
                loaddatatotable($('#nhomchitieu').val(), $('#nhomdonvi').val(), stus, initData, $.session.get('nkhid'), 0);
            } else {
                fl = 1;
                $('#nhomdonvi').prop('disabled', 'disabled');
                $('#donvi').prop('disabled', false);
                loaddatatotable($('#nhomchitieu').val(), $('#donvi').val(), stus, initData, $.session.get('nkhid'), fl);
            }
        })
        $('#nhomdonvi').on('change', function () {
            loaddatatotable($('#nhomchitieu').val(), $('#nhomdonvi').val(), stus, initData, $.session.get('nkhid'), 0);
        });
        $('#donvi').on('change', function () {
            loaddatatotable($('#nhomchitieu').val(), $('#donvi').val(), stus, initData, $.session.get('nkhid'), 1);
        });
        $('#nhomchitieu').on('change', function () {
            if (fl == 0) {
                loaddatatotable($('#nhomchitieu').val(), $('#nhomdonvi').val(), stus, initData, $.session.get('nkhid'), fl);
            } else {
                loaddatatotable($('#nhomchitieu').val(), $('#donvi').val(), stus, initData, $.session.get('nkhid'), fl);
            }

        });
        $('#Chot').live('click', function () {
            var r = confirm("Đây là thao tác chốt số liệu cho TẤT CẢ CÁC ĐƠN VỊ và gửi về ban KHTC. Sau khi chốt gửi Ban KHTC các đơn vị sẽ không thể sửa đổi.\n Bạn có chắc muốn thực hiện không ?");
            if (r == true) {
                $.ajax({
                    url: initData.CheckCommnetchot,
                    type: "GET",
                    headers: {
                        'VerificationToken': initData.VerificationToken
                    },
                    dataType: "json",
                    async: false,
                    data: {
                        'namkh': $.session.get('nkhid')
                    },
                    success: function (data) {
                        if (data == 0) {
                            $.ajax({
                                url: initData.CommentChot,
                                type: "POST",
                                headers: {
                                    'VerificationToken': initData.VerificationToken
                                },
                                dataType: "json",
                                async: false,
                                data: {
                                    'namkh': $.session.get('nkhid')
                                },
                                success: function (data) {
                                    window.notice('Bạn đã gửi thành công thành công. ', window.notice_success);
                                    loadtable($.session.get('nkhid'), initData);
                                    $('#myModalNew').modal('hide');
                                },
                                error: function (jqXHR, textStatus, errorThrown) {
                                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                                },
                                complete: function () {

                                }
                            });
                        } else {
                            var r = confirm("Vẫn còn đơn vị chưa phải hồi ! Bạn có muốn gửi lên Ban KHTC ?");
                            if (r == true) {
                                $.ajax({
                                    url: initData.CommentChot,
                                    type: "POST",
                                    headers: {
                                        'VerificationToken': initData.VerificationToken
                                    },
                                    dataType: "json",
                                    async: false,
                                    data: {
                                        'namkh': $.session.get('nkhid')
                                    },
                                    success: function (data) {
                                        window.notice('Bạn đã gửi thành công thành công. ', window.notice_success);
                                        loadtable($.session.get('nkhid'), initData);
                                        $('#myModalNew').modal('hide');
                                    },
                                    error: function (jqXHR, textStatus, errorThrown) {
                                        window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                                    },
                                    complete: function () {

                                    }
                                });
                            }
                        }
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                    },
                    complete: function () {

                    }
                });
            }
        })
        $('#HuyChot').live('click', function () {
            var r = confirm("Bạn có muốn thực hiện việc hủy số liệu đã gửi cho Ban KHTC không?");
            if (r == true) {
                $.ajax({
                    url: initData.CommentHuychot,
                    type: "GET",
                    headers: {
                        'VerificationToken': initData.VerificationToken
                    },
                    dataType: "json",
                    async: false,
                    data: {
                        'namkh': $.session.get('nkhid')
                    },
                    success: function (data) {
                        if (data > 0) {
                            window.notice('Bạn đã hủy thành công. ', window.notice_success);
                            loadtable($.session.get('nkhid'), initData);
                        } else {
                            window.notice('Bạn đã hủy thất bại !', window.notice_error);
                        }
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                    },
                    complete: function () {

                    }
                });
            }
            })
        $('input[level="2"]').live('dblclick', function () {
            var chitieu = $(this).attr('chitieuid');
            var donvi = $(this).attr('donviid');
            $.ajax({
                url: initData.GetallHistoryComment,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                data: {
                    'namkh': $('#Namlapbc').val(),
                    'moc': '2',
                    'mocks': -1,
                    'flag': 0,
                    'dv': donvi,
                    'ct': chitieu
                },
                success: function (data) {
                    $('#chatbox').empty();
                    if (data.length > 0) {
                        $.each(data, function (i, v) {
                            if (parseInt(v.BAN_ID) != 0) {
                                $('#chatbox').append('<div class="arrow_box_left">\
                                                   <span>' + v.COMMENT_BAN + '</span><br />\
                                                   <span style="float: right;">' + convertdatatime(v.LAST_UPDATE) + '</span></div>');
                            } else {
                                $('#chatbox').append('<div class="arrow_box_right">\
                                                   <span>' + v.COMMENT_DV + '</span><br />\
                                                   <span style="float: left;">' + convertdatatime(v.LAST_UPDATE) + '</span></div>');
                            }
                        });
                        $('#myModalHistoryComment').modal('show');
                    } else {
                        window.notice('Chỉ tiêu không có lịch sử nào !', window.notice_waring);
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {
                }
            });
        })

    }
    return {
        init: function (initData) {
            //load data
            initLoadData(initData);
            initButtonClick(initData);
        }
    }
}();
